<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\WorksUpload;

/**
 * Class ShowWorks
 * @package App\Models
 * @@property int $id
 * @property int $works_id
 * @property int $company_id
 * @property int $type_id
 * @property int $vote_award_id
 * @property string $works_cover
 * @property int $sort
 */

class ShowWorks extends Model
{
    protected $table = 'xyj_show_works';

    /**
     * 作品
     */
    public function works()
    {
        return $this->hasOne('\App\Models\Works', 'id' , 'works_id');
    }

    /**
     * 附件
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function worksUploads()
    {
        return $this->hasMany('\App\Models\WorksUpload', 'works_id' , 'works_id');
    }

    public function worksTexts()
    {
        return $this->hasOne('\App\Models\WorksText', 'works_id' , 'works_id');
    }


    /**
     * 调取赛事名称
     * @param $award_id
     * @return string
     */
    public function getAwardName($award_id)
    {
        return Award::find($award_id)->name;
    }

    /**
     * 调取命题企业名称
     * @param $company_id
     * @return string
     */
    public function getCompanyName($company_id)
    {
        return PropositionCompany::find($company_id)->name;
    }

    /**
     * 调取作品类别名称
     * @param $type_id
     * @return string
     */
    public function getWorksTypeName($type_id)
    {
        return WorksType::find($type_id)->name;
    }

    /**
     * 调取奖项名称
     * @param $vote_award_id
     * @return string
     */
    public function getVoteAwardName($vote_award_id)
    {
        return VoteAward::find($vote_award_id)->name;
    }
    /**
     * 调取作品名称
     * @param $award_id
     * @return string
     */
    public function getWorksName($works_id)
    {
        return Works::find($works_id)->name;
    }

    /**
     * 获取缩略图
     * @return string
     */
    public function getThumbUrl()
    {
        try {
            if ($this->works_cover) {
                return $this->works_cover;
            } elseif ($url = $this->generateThumb()) {
                return $url;
            } else {
                return url('assets/thumb/none.png');
            }
        } catch (\Exception $e) {
            \Log::error('生成作品缩略图出错,' . $e->getMessage(), $this->toArray());
        }
    }



    /**
     * 生成缩略图
     * @return string
     */
    public function generateThumb()
    {
        try {
            $thumbUrl = null;
            if ($this->works->worksType->templet == 3){
                $thumbUrl = url('assets/thumb/wenan.png');
            }elseif ($this->works->worksType->templet == 4){
                $cehua = WorksUpload::where(['works_id' => $this->works_id , 'type' => '封面'])->first();
                if ($cehua){
                    $thumbUrl = $cehua->getRateThumbUrl();
                }else{
                    return '';
                }
            }else{
                if ($this->worksUploads->count() > 0) {
                    $upload = $this->worksUploads->first();
                    switch ($upload->extension) {
                        case "avi":
                            $thumbUrl = $upload->getVideoThumbUrl();
                            break;
                        case "doc":
                            $thumbUrl = url('assets/thumb/doc.png');
                            break;
                        case "docx":
                            $thumbUrl = url('assets/thumb/docx.png');
                            break;
                        case "flv":
                            $thumbUrl = $upload->getVideoThumbUrl();
                            break;
                        case "mp3":
                            $thumbUrl = url('assets/thumb/mp3.png');
                            break;
                        case "mp4":
                            $thumbUrl = $upload->getVideoThumbUrl();
                            break;
                        case "pdf":
                            $thumbUrl = url('assets/thumb/pdf.png');
                            break;
                        case "swf":
                            $thumbUrl = url('assets/thumb/swf.png');
                            break;
                        case "tiff":
                            $thumbUrl = url('assets/thumb/tiff.png');
                            break;
                        case "url":
                            $thumbUrl = url('assets/thumb/url.png');
                            break;
                        case "wmv":
                            $thumbUrl = $upload->getVideoThumbUrl();
                            break;
                        default:
                            $thumbUrl = $upload->getRateThumbUrl();
                            break;
                    }
                }
            }
            $this->works_cover = $thumbUrl;
            $this->save();
            $url = $this->works_cover;
            return $url;
        } catch (\Exception $e) {
            \Log::error('生成作品缩略图出错,' . $e->getMessage(), $this->toArray());
        }
    }
}
